﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using TestBAT;
using System.IO;
using System.Text.RegularExpressions;

public partial class _Default : System.Web.UI.Page
{
    TestBATExecution exBAT = null; 
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString.ToString() != "" && Request.QueryString != null)
            {
                if (!string.IsNullOrEmpty(Request.QueryString["rptLink"].ToString()) && !string.IsNullOrEmpty(Request.QueryString["projName"].ToString()))
                {
                    Session["RptXMLPath"] = Request.QueryString["rptLink"].ToString();
                    Session["ProjName"] = Request.QueryString["projName"].ToString();
                    string strPath = ConfigurationManager.AppSettings["InitialPage"] + "?rptLink=" + Request.QueryString["rptLink"].ToString();
                    //Server.Transfer(strPath);
                    Response.Redirect(strPath);
                }
            }
            else
            {
                //drpCovSource.Items.Add(System.Configuration.ConfigurationManager.AppSettings["XMLPath"]);
                //drpCovSource.Items.Add(System.Configuration.ConfigurationManager.AppSettings["XMLPath1"]);
                ////Session["RptXMLPath"] = ConfigurationManager.AppSettings["XMLPath"];
                ////string strPath = ConfigurationManager.AppSettings["InitialPage"];
                ////Server.Transfer(strPath);
                if(Directory.Exists(ConfigurationManager.AppSettings["SourceDir"]))
                {
                    TestBATExecution tbat = new TestBATExecution();
                    List<FileNames> fNames = tbat.GetCoverageSource(ConfigurationManager.AppSettings["SourceDir"].ToString());
                    drpCovSource.Items.Insert(0, "Select coverage source");
                    foreach (FileNames fl in fNames)
                    {
                        drpCovSource.Items.Add(fl.FileName); 
                    }
                }

            }   


        }
    }
    protected void drpCovSource_SelectedIndexChanged(object sender, EventArgs e)
    {
        string strPath = string.Empty;
        int intItem = drpCovSource.SelectedIndex; 
        
            if (drpCovSource.SelectedIndex != 0)
            {
                Session["RptXMLPath"] = ConfigurationManager.AppSettings["SourceDir"] + drpCovSource.SelectedItem.Text;
                if (intItem == 1)
                {
                    Session["ProjName"] = "Composite Application Library";
                }
                else if (intItem == 2 || intItem == 3 || intItem == 4 || intItem == 6)
                {
                    Session["ProjName"] = "Sample Application";
                }
                else if (intItem == 5 || intItem == 7)
                {
                    Session["ProjName"] = "Model View Controller";
                }

                strPath = ConfigurationManager.AppSettings["InitialPage"];
                Server.Transfer(strPath);
            }
        
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        TestBATExecution tb = new TestBATExecution();
        string strAppPath = string.Empty;
        string strXMLpath = tb.ExecuteCoverage(txtTestContainer.Text.Trim(), txtRunConfig.Text.Trim(), txtResultsFile.Text.Trim(), txtConfigName.Text.Trim(),txtFileExtractionPath.Text.Trim());
        if (!string.IsNullOrEmpty(strXMLpath))
        {
            if (ValidFilePath(strXMLpath))
            {
                Session["RptXMLPath"] = strXMLpath;
                strAppPath = ConfigurationManager.AppSettings["InitialPage"];
                if (!string.IsNullOrEmpty(strAppPath))
                {
                    Server.Transfer(strAppPath);
                }
            }
            else
            {
                //ResetTextBoxes();
                ClientScript.RegisterStartupScript(GetType(), "myscript", "<script>alert('Report file is not proper. Please check with your input parameters.');</script>");
            }
        }
        else
        {
            ClientScript.RegisterStartupScript(GetType(), "myalert", "<script>alert('Coverage process is not succeeded. You may need to check user-defined scheme of your testrunconfig or provide the same scheme name in user-defined scheme name parameter');</script>");
        }

        //string a = tb.ExecuteCoverage(@"C:\Users\knaray\Documents\Visual Studio 2008\Projects\empl\empTest\bin\Debug\empTest.dll", @"C:\Users\knaray\Documents\Visual Studio 2008\Projects\empl\LocalTestRun.testrunconfig", @"c:\tobedeleted\result\test.trx", "Yuvaraj", "BANGBC");
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        ResetTextBoxes();
        
    }
    public bool ValidFilePath(string path)
    { 
        String pattern = @"^(([a-zA-Z]\:)|(\\))(\\{1}|((\\{1})[^\\]([^/:*?<>""|]*))+)$"; 
        Regex reg = new Regex(pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase); 
        return reg.IsMatch(path);
    }
    public void ResetTextBoxes()
    {
        txtTestContainer.Text = "";
        txtRunConfig.Text = "";
        txtResultsFile.Text = "";
        txtConfigName.Text = "";        
        txtFileExtractionPath.Text = "";
        txtTestContainer.Focus();
    }
    protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
    {
        args.IsValid = ValidFilePath(txtTestContainer.Text.Trim());
    }
    protected void CustomValidator1_ServerValidate1(object source, ServerValidateEventArgs args)
    {
        args.IsValid = (args.Value.Length >= 5);
    }
}

